package de.logicline.cvss.ui.view.table;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;

import de.logicline.cvss.vo.CVSSearchEntry;

public class TableSorter extends ViewerSorter {

	public static final int SORT_DATE = 1;
	public static final int SORT_AUTHOR = 2;
	public static final int SORT_COMMENT = 3;
	
	
	private int sortByCriteria;
	private boolean topDown;
	
	public TableSorter(int sortBy, boolean sortDirection) {
		this.topDown = sortDirection;
		this.sortByCriteria = sortBy;
	}
	
	public int compare(Viewer viewer, Object e1, Object e2) {
		
		int result = 0;
		
		if (e1 instanceof CVSSearchEntry
				&& e2 instanceof CVSSearchEntry) {
			CVSSearchEntry entry1 = (CVSSearchEntry) e1;
			CVSSearchEntry entry2 = (CVSSearchEntry) e2;
			switch (sortByCriteria) {
				case SORT_DATE :
					result = entry1.getDate().compareTo(entry2.getDate());
					
				case SORT_AUTHOR :
					result = entry1.getAuthor().compareTo(entry2.getAuthor());
										
				case SORT_COMMENT :
					result = entry1.getComment().compareTo(entry2.getComment());
					
			}
		}
		
		if (!topDown) {
			result *= -1; // turn it around
		}
		return result;
	}
}
